/**
 * Created by zy on 2016/6/13.
 */
;(function ($, window, document, undefined) {

    //标准编号
    var $standardCode = $('#JQ_standard_code');
    //标准名称
    var $standardName = $('#JQ_standard_name');
    //附件id
    var $attachmentId = $('#attachmentId');
    var $radioType = $('#radioType');
    var $standardId = $('#standardId');
    var $upFile = $('#up-file');

    var filter = {
        getParam : function(){
            var data = {};
            data.mode=true;
            data.id = $standardId.val();
            data.name = function() {
               var v = $standardName.val();
                if(!v) {
                    $standardName.TIP({content : '标准名称不能为空！'});
                    data.mode= false;
                    return '';
                }
                return v;
            }();
            data.code = function() {
                var v = $standardCode.val();
                if(!v) {
                    $standardCode.TIP({content : '标准编号不能为空！'});
                    data.mode=false;
                    return '';
                }
                return v;
            }();
            data.attachmentId = function(){
                var v = $attachmentId.val();
                if(!v){
                    $upFile.TIP({content:'请上传文件！'});
                    data.mode=false;
                    return '';
                }
                return v;
            }();
            data.type = $radioType.find('i.active').attr('data-value');
            return data;
        }
    };

    //返回
    $('#JQ_goback').on('click', function () {
        location.href = DyyBase.root_path + '/docmger/standard/toPage.html';
    });

    var uploader;
    function initUploader() {
        var $this_button = $('#JQ_standard_upload');
        var dataParam = {};
        //dataParam.businessId = $comId.val();
        dataParam.onlyOne = true;
        dataParam.type = $this_button.attr('data-type');
        var dom_btn = $this_button.get(0);
        var wind;

        // 实例化一个plupload上传对象
        uploader = new plupload.Uploader({
            browse_button: dom_btn,
            url: DyyBase.url_upload_file(),
            filters: {
                mime_types: [
                    {title:'files', extensions: 'xls,xlsx,doc,docx,pdf'}
                ],
                max_file_size : '10mb'
            },
            multipart_params: dataParam,
            multi_selection:false,
            flash_swf_url: DyyBase.root_path+'/static/js/plugin/plupload/Moxie.swf',
            silverlight_xap_url: DyyBase.root_path+'/static/js/plugin/plupload/Moxie.xap',
            init: {
                BeforeUpload: function(u,f){
                    wind = $this_button.WIND({
                        width: 750,
                        height: 240,
                        title: '上传',
                        actionBox: 'div.plan-box',
                        closeBtn: '',
                        yesBtn: 'JQ_action_yes_btn',
                        noBtn: 'JQ_action_no_btn',
                        yesBtnTxt: '保存',
                        noBtnTxt: '取消',
                        yesBtnAct: function(){
                            $attachmentId.val(uploaderAfterAttaId);
                            $upFile.val(uploaderAfterFileName);
                            wind.clear();
                        },
                        noBtnAct: function(){
                            uploader.stop();
                            uploaderAfterFileName = '';
                            uploaderAfterAttaId = '';
                            wind.clear();
                        }
                    });
                },
                UploadProgress: function(u,f){
                    $('.up-name').html(f.name);
                    //进度条显示
                    var precent = f.percent + '%';
                    $('.process-no').html(precent);
                    var $pro=$('.up-process em');
                    $pro.css({width : precent});
                },
                FileUploaded: function(u,f,info){
                    showUploadImg(info,f.name);
                },
                FilesAdded: function(u,f){uploader.start();},
                UploadComplete: function(u,f){},
                Error: function(u,f){
                    if(f.code == -600) {
                        var maxSizeStr = uploader.settings.filters.max_file_size.toUpperCase();
                        var msg = f.file.name + '&nbsp;&nbsp;文件太大超出限制<p>请上传' + maxSizeStr+ '以内的文件!</p>';
                        $this_button.MSG({
                            type: 'error',
                            content: msg,
                            contentTitle: f.file.name,
                            contentAddon: '&nbsp;&nbsp;文件太大超出限制!',
                            contentMust: '<p>请上传 '+ maxSizeStr +'以内的文件!</p>'
                        });
                    }
                    if(f.code == -601){
                        var msg = f.file.name + '&nbsp;&nbsp;文件格式不正确!<p>请上传 (xls,xlsx,doc,docx,pdf) 格式文件!</p>';
                        $this_button.MSG({
                            type: 'error',
                            content: msg,
                            contentTitle: f.file.name,
                            contentAddon: '&nbsp;&nbsp;文件格式不正确!',
                            contentMust: '<p>请上传 (xls,xlsx,doc,docx,pdf) 格式文件!</p>'
                        });
                    }
                }
            }
        });
        // 初始化
        uploader.init();
    }

    initUploader();

    var uploaderAfterFileName,uploaderAfterAttaId;
    // 添加展示上传完成后的图片
    function showUploadImg(o,name){
        var json = $.parseJSON(o.response);
        uploaderAfterAttaId = json.result.id;
        uploaderAfterFileName = name;
    }

    $('#JQ_save').click(function() {

        var $this = $(this);
        var data = filter.getParam();
        var url;
        if(data.id) {
            url = docApi.url_doc_edit();
        } else {
            url = docApi.url_doc_add();
        }
        if(data.mode) {
            DyyBase.ajax(url, data, function (dataset) {
                if (dataset.bool) {
                    $this.MSG({
                        type: 'success',
                        content:function(){
                            if(data.id) {
                                return '修改成功';
                            } else {
                                return '保存成功！';
                            }
                        }(),
                        yesBtnAct: function () {
                            location.href = DyyBase.root_path + '/docmger/standard/toPage.html';
                        }
                    });
                } else {
                    $this.MSG({
                        type: 'error',
                        content: dataset.result
                    });
                }
            });
        }
    });


})(jQuery, window, document);